package com.xywh.wanandroid.compose_ui.main.bottomBar

import android.util.Log
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.pullrefresh.PullRefreshIndicator
import androidx.compose.material.pullrefresh.pullRefresh
import androidx.compose.material.pullrefresh.rememberPullRefreshState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.paging.compose.LazyPagingItems
import androidx.paging.compose.collectAsLazyPagingItems
import com.xywh.composeLibrary.ext.ListFloatingActionButton
import com.xywh.composeLibrary.statex.StateCompose
import com.xywh.wanandroid.compose_ui.main.ArticleItem
import com.xywh.wanandroid.compose_ui.main.cancelOrAddCollectArticle
import com.xywh.wanandroid.data.model.bean.ArticleItem
import com.xywh.wanandroid.data.model.bean.BannerBean
import com.xywh.wanandroid.viewmodel.MainComposeViewModel
import com.zj.banner.BannerPager
import com.zj.banner.ui.config.BannerConfig
import me.hgj.jetpackmvvm.ext.util.loge


@Composable
fun HomeScreen(
    viewModel: MainComposeViewModel,
    innerPadding: PaddingValues,
    navigateWebView: (String, String) -> Unit
) {
    val banners =
        viewModel.banners.collectAsLazyPagingItems()



    StateCompose(banners) {

        //    val topArticles = viewModel.topArticles.collectAsLazyPagingItems()
        val topArticles = viewModel.collectTopArticlesLiveData.collectAsLazyPagingItems()
//    val articles = viewModel.articles.collectAsLazyPagingItems()
        val articles = viewModel.collectArticlesLiveData.collectAsLazyPagingItems()
//        val articles = viewModel.collectSquareArticlesLiveData.collectAsLazyPagingItems()

        HomeContent(
            innerPadding,
            viewModel,
            banners,
            topArticles,
            articles,
            navigateWebView
        ) { id, collect ->
            cancelOrAddCollectArticle(collect, viewModel, id)
        }
    }
}

@Composable
@OptIn(ExperimentalMaterialApi::class)
fun HomeContent(
    innerPadding: PaddingValues,
    viewModel: MainComposeViewModel,
    banners: LazyPagingItems<BannerBean>,
    topArticles: LazyPagingItems<ArticleItem>,
    articles: LazyPagingItems<ArticleItem>,
    navigateWebView: (String, String) -> Unit,
    favoriteOnClick: (Int, Boolean) -> Unit
) {


    val refresh by remember { mutableStateOf(false) }
    val pullRefreshState = rememberPullRefreshState(refresh, {
        banners.refresh()
        topArticles.refresh()
        articles.refresh()
    })
    val listState = rememberLazyListState()

    val scope = rememberCoroutineScope()

    "HomeContentHomeContentHomeContentHomeContent".loge("=======================HomeContent====")
    Box(
        modifier = Modifier
            .fillMaxSize()
            .pullRefresh(pullRefreshState)
    ) {

        Column(Modifier.fillMaxSize()) {

            if (banners.itemSnapshotList.items.isNotEmpty())
                BannerPager(
                    items = banners.itemSnapshotList.items,
                    config = BannerConfig(
                        bannerImagePadding = 0.dp,
                        shape = RoundedCornerShape(0)
                    )
                ) { item ->
                    Log.e("TAG", "Notes: 哈哈哈:$item")
                }

            LazyColumn(
                state = listState,
                modifier = Modifier
                    .fillMaxWidth()
                    .fillMaxHeight()
            ) {

                items(
                    count = topArticles.itemCount,
                    key = { index ->
                        val article = topArticles[index]
                        "${article?.id ?: ""}${index}"
                    }
                ) {
                    val itemData = topArticles[it] ?: return@items
                    ArticleItem(
                        itemData,
                        true,
                        favoriteOnClick = favoriteOnClick
                    ) { title, link ->
                        navigateWebView.invoke(title, link)
                    }
                }


                items(
                    count = articles.itemCount,
                    key = { index ->
                        val article = articles[index]
                        "${article?.id ?: ""}${article?.title ?: ""}${index}"
                    }) {
                    val itemData = articles[it] ?: return@items
                    ArticleItem(itemData, false, favoriteOnClick = favoriteOnClick) { title, link ->
                        navigateWebView.invoke(title, link)
                    }
                }
            }
        }

        Box(
            Modifier
                .padding(10.dp)
                .align(Alignment.BottomEnd)
        ) {
            ListFloatingActionButton(scope, listState)
        }
        PullRefreshIndicator(
            refresh,
            pullRefreshState,
            Modifier.align(Alignment.TopCenter),
            scale = true
        )

    }
}
